import mx.collections.ArrayCollection;
import mx.rpc.AsyncToken;
import mx.rpc.AbstractService;
import mx.rpc.soap.mxml.WebService;
import mx.rpc.events.ResultEvent;
import mx.events.*;
import mx.managers.DragManager;
import mx.core.UIComponent;
import mx.controls.List;
import mx.controls.Label;
import mx.controls.TextInput;
import mx.containers.VBox;
import trackstacks.ServiceLocator;
import trackstacks.TokenResponder;

[Bindable]
private var filterAC: ArrayCollection;
[Bindable]
private var issueAC: ArrayCollection;
[Bindable]
private var selectedIssue: Object;
private var sharedObj: SharedObject;

private function initApp(): void {
    sharedObj = SharedObject.getLocal("trackstacks");
    tiURL.text = sharedObj.data.url;
    tiLogin.text = sharedObj.data.login;
    tiPassword.text = sharedObj.data.password;
}

private function doLogin(): void {
    sharedObj.data.url = tiURL.text;
    sharedObj.data.login = tiLogin.text;
    sharedObj.data.password = tiPassword.text;
    sharedObj.flush();
    ServiceLocator.jiraURL = tiURL.text;
    var service:WebService = ServiceLocator.getJiraService();
    lblResult.text = "Logging in...";
    var token:AsyncToken = AsyncToken(service.login(tiLogin.text, tiPassword.text));
    token.addResponder(new TokenResponder(onLoginSuccess));
}

private function onLoginSuccess(event:ResultEvent):void {
    ServiceLocator.jiraLoginToken = event.result as String;
    lblResult.text = "Loading saved filters...";
    var service:WebService = ServiceLocator.getJiraService();
    var token:AsyncToken = AsyncToken(service.getSavedFilters(ServiceLocator.jiraLoginToken));
    token.addResponder(new TokenResponder(onGetSavedFilters));
}

private function onGetSavedFilters(event: ResultEvent):void {
    filterAC = event.result as ArrayCollection;
    mainViewStack.selectedIndex = 1;
}

private function showMainView(): void {
    lblResult2.text = "Loading issues...";
    var service:WebService = ServiceLocator.getJiraService();
    var filter:String = cmbFilter.selectedItem.id;
    var token:AsyncToken = AsyncToken(service.getIssuesFromFilter(ServiceLocator.jiraLoginToken, filter));
    token.addResponder(new TokenResponder(onGetIssues));
}

private function onGetIssues(event: ResultEvent): void {
    issueAC = event.result as ArrayCollection;
    mainViewStack.selectedIndex = 2;
}

private function issueLabelFunction(issue: Object): String {
    return issue.key + " " + issue.summary;
}

private function doDragEnter(event: DragEvent): void {
    DragManager.acceptDragDrop(event.currentTarget as UIComponent);
}

private function doDragOver(event: DragEvent): void {
    DragManager.acceptDragDrop(event.currentTarget as UIComponent);
}

private function doDragDrop(event: DragEvent): void {
    var issueStack: IssueStack = event.dragSource.dataForFormat("issueStack") as IssueStack;
    if (issueStack != null) {
        issueStack.x = event.localX - issueStack.getDragOffsetX();
        issueStack.y = event.localY - issueStack.getDragOffsetY();
        return;
    }

    var newStack: IssueStack = new IssueStack();
    newStack.x = event.localX;
    newStack.y = event.localY;
    pnlStacks.addChild(newStack);
    var dragObj: Array = event.dragSource.dataForFormat("items") as Array;
    for(var ii:String in dragObj) {
        var issue:Object = dragObj [ii];
        var index: int = issueAC.getItemIndex(issue);
        if (index >= 0) {
            issueAC.removeItemAt(index);
        }
    }
    newStack.addEventListener("issueSelected", updateSelectedIssueStack);
    newStack.initStack(dragObj);
}

private function updateSelectedIssue(event: ListEvent): void {
    selectedIssue = (event.currentTarget as List).selectedItem;
}

private function updateSelectedIssueFocus(event: FocusEvent): void {
    selectedIssue = (event.currentTarget as List).selectedItem;
}

private function updateSelectedIssueStack(event: Event): void {
    selectedIssue = (event.currentTarget as IssueStack).getSelectedIssue();
}
